<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/24
 * Time: 22:54
 */

namespace App\HttpController\Admin;


use EasySwoole\Core\Http\AbstractInterface\Controller;
use EasySwoole\Core\Swoole\Process\ProcessManager;
use EasySwoole\Core\Component\Cache\Cache as CacheC;
class Cache Extends Controller
{

    public function index()
    {

    }

    public function refresh()
    {
        $type = $this->request()->getRequestParam('type');

        if ($type == 'rate') {
            ProcessManager::getInstance()->writeByProcessName('RefreshCache', 'refreshRate');
        }
        if ($type == 'room') {
            ProcessManager::getInstance()->writeByProcessName('RefreshCache', 'refreshRoom');
        }

        $this->response()->write("  ".'【API系统缓存已更新】');


    }


    public function getCache()
    {
        $type = $this->request()->getRequestParam('type');
        if ($type == 'rate') {
            $data['game'] = CacheC::getInstance()->get('game_rate');
            $data['return'] = CacheC::getInstance()->get('return_rate');
            $this->success($data);
        }
        if ($type == 'room') {
            $room_list = CacheC::getInstance()->get('room_list');
            $this->success($room_list);
        }


    }

    protected function success($data = null, $msg = '')
    {
        if (!$this->response()->isEndResponse()) {
            $data = Array(
                "ResponseStatus" => 0,
                "datas" => $data,
                "ResponseMsg" => $msg,

            );
            $this->response()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
            return true;
        } else {
            trigger_error("response has end");
            return false;
        }
    }


    protected function onRequest($action): ?bool
    {


        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }

}